//
//  BBLinkViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/9.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import MJRefresh

let BBLinkReuseIdentifier = "BBLinkReuseIdentifier"
class BBLinkViewController: BBBaseViewController {

    var currentPageNum:Int = 1
    var links:[BBLink]?{
        didSet{
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 1.如果没有登录, 就设置未登录界面的信息
        if !userLogin
        {
            visitorView?.setupVisitorInfo(animationType:2, imageName: "Visitor_link_back", message: "Meet like-minded people!")
            return
        }
       view.addSubview(tableView)
        //注册一个cell
        tableView.register(BBLinkTableViewCell.self, forCellReuseIdentifier: BBLinkReuseIdentifier)
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header.setRefreshingTarget(self, refreshingAction: #selector(refreshData))
        tableView.mj_footer = MJRefreshAutoNormalFooter()
        tableView.mj_footer.setRefreshingTarget(self, refreshingAction: #selector(loadMore))
        refreshData()
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "link_write",target: self,action: #selector(rightItemClick))
    }
    
    @objc private func refreshData(){
        currentPageNum = 1
        BBLink.loadLinks(pageNum: currentPageNum) { (links, error) in
            self.links = links
            self.tableView.mj_header.endRefreshing()
        }
    }
    
    @objc private func loadMore(){
        currentPageNum += 1
        BBLink.loadLinks(pageNum: currentPageNum) { (links, error) in
            if links?.count != 0{
                self.links = self.links! + links!
            }
            self.tableView.mj_footer.endRefreshing()
        }
    }
    
    @objc private func rightItemClick(){
        let vc = BBWriteLinkViewController.init(nibName: "BBWriteLinkViewController", bundle: nil)
        self.navigationController!.pushViewController(vc, animated: true)
    }
    
    private lazy var tableView:UITableView = {
        let tableView = UITableView.init(frame: UIScreen.main.bounds)
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
       // tableView.separatorStyle = .none
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()
}

extension BBLinkViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return links?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: BBLinkReuseIdentifier, for: indexPath) as! BBLinkTableViewCell
        let link = links![indexPath.row]
        cell.link = link
        return cell
    }
}
